<template>
  <div id="app">

    <el-button type="text" @click="dialogVisible = true">点击添加学生资料</el-button>

    <el-dialog
  title="提示"
  :visible.sync="dialogVisible"
  width="30%"
  :before-close="handleClose">
  <el-form v-model="form">
    <el-form-item label="ID">
      <el-input v-model="form.id"></el-input>
    </el-form-item>
    <el-form-item label="姓名">
      <el-input v-model="form.name"></el-input>
    </el-form-item>
    <el-form-item label="成绩">
      <el-input v-model="form.achievement"></el-input>
    </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="createStudent">确 定</el-button>
  </span>
</el-dialog>

  <el-table :data="studentList" max-height="250">
    <el-table-column
      fixed
      prop="id"
      label="ID"
      width="150">
    </el-table-column>
    <el-table-column
      prop="name"
      label="姓名"
      width="120">
    </el-table-column>
    <el-table-column
      prop="achievement"
      label="成绩"
      width="120">
    </el-table-column>
    <el-table-column
      label="操作"
      width="120">
      <template slot-scope="scope">
        <el-button
          @click.native.prevent="del(scope.row.id)"
          type="text"
          size="small">
          删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  </div>
</template>

<script>
import axios from "axios"

export default {

  methods: {
    createStudent(done){
      if(this.form.id !== ""){
        axios.post("http://127.0.0.1:7001/students",{
          student : this.form
        }).then(() => {
          this.getData()
          this.dialogVisible = false
          console.log(this.form)
        })
      }else{
        this.getData()
          this.$confirm('请输入学生信息')
          .then(() => {
            done();
          })
          .catch(() => {});
      }
    },
    getData(){
      axios.get("http://127.0.0.1:7001/students").then(res => {
        this.studentList = res.data;
      })
    },
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(() => {
            done();
          })
          .catch(() => {});
      },
      del(id) {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // rows.splice(index, 1);
          axios.delete(`http://127.0.0.1:7001/students/${id}`).then(() => {
            this.getData()
            this.$message({
              type: 'success',
              message: '删除成功!',
            });
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      },

    },
    data() {
      return {
        studentList: [],
        dialogVisible: false,
        form:{
          id:"",
          name:"",
          achievement:""
        }
      }
    },
    created(){
      this.getData();
    },
}
</script>

<style>

</style>
